package com.thor.palmmall.rest.inspect;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.commons.util.ConverterUtil;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.inspect.converter.ACategoryConverter;
import com.thor.palmmall.rest.inspect.converter.AInspectBillConverter;
import com.thor.palmmall.rest.inspect.converter.SInspectBill4SaveConverter;
import com.thor.palmmall.rest.inspect.converter.SInspectBillConverter;
import com.thor.palmmall.rest.inspect.converter.SInspectBillQueryParamConverter;
import com.thor.palmmall.spi.basic.SBasicService;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.inspect.SCategory;
import com.thor.palmmall.spi.inspect.SInspectBill;
import com.thor.palmmall.spi.inspect.SInspectBillService;

public class AInspectServiceImpl implements AInspectService {

  @Autowired
  private SBasicService sBasicService;
  @Autowired
  private SInspectBillService sInspectBillService;

  @Override
  public String create(AInspectBill bill, BeanOperateInfo operateInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");

    SResponse<String> resp = sInspectBillService.create(
        SInspectBillConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法创建运营巡检单：" + resp.getMessage());
  }

  @Override
  public AInspectBill get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    SResponse<SInspectBill> resp = sInspectBillService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess()) {
      return AInspectBillConverter.getInstance().convert(resp.getBody());
    } else {
      throw new ThorServiceException("无法找到指定ID的运营巡检单：" + resp.getMessage());
    }
  }

  @Override
  public void save(AInspectBill bill, BeanOperateInfo operateInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");
    SResponse<Void> resp = sInspectBillService.save(
        SInspectBill4SaveConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess()) {
      return;
    } else {
      throw new ThorServiceException("无法保存运营巡检单：" + resp.getMessage());
    }
  }

  @Override
  public QueryResult<AInspectBill> query(AInspectBillQueryParam param) throws ThorServiceException {
    Assert.assertArgumentNotNull(param, "param");

    SResponse<SQueryResult<SInspectBill>> resp = sInspectBillService
        .query(SInspectBillQueryParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(), AInspectBillConverter.getInstance());
    else
      throw new ThorServiceException("无法查询运营巡检单：" + resp.getMessage());
  }

  @Override
  public List<ACategory> getCategories() throws ThorServiceException {
    SResponse<List<SCategory>> resp = sBasicService.inspectCategories();
    if (resp.isSuccess()) {
      return ConverterUtil.convert(resp.getBody(), ACategoryConverter.getInstance());
    } else {
      throw new ThorServiceException("没有查询到可用的一级运营巡检分类：" + resp.getMessage());
    }
  }

  @Override
  public List<ACategory> getCategory(String uuid) throws ThorServiceException {
    SResponse<List<SCategory>> resp = sBasicService.getCategory(uuid);
    if (resp.isSuccess()) {
      return ConverterUtil.convert(resp.getBody(), ACategoryConverter.getInstance());
    } else {
      throw new ThorServiceException("没有查询到可用的二级运营巡检分类：" + resp.getMessage());
    }
  }

}
